package com.xyd.controller;

import com.github.pagehelper.Page;
import com.xyd.entity.Result;
import com.xyd.entity.User;
import com.xyd.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import java.util.HashMap;
import java.util.Map;

@RestController
public class UserController {

    @Autowired
    private UserService userService;

    @RequestMapping("/toList")
    public ModelAndView toList() {
        return new ModelAndView("list");
    }

    @RequestMapping("/getPage")
    public Map queryUserByPage(int pageNum, int pageSize, User user) {
        Map map = new HashMap();
        map.put("pageSize", pageSize);
        map.put("pageNum", pageNum);
        map.put("user", user);
        Page page = userService.queryLikeUsers(map);
        map.put("page", page);
        map.put("totalPages", page.getPages());
        return map;
    }

    @RequestMapping("/add")
    public Result addUser(User user) {
        userService.addUser(user);
        return new Result();
    }

    @RequestMapping("/del")
    public Result delUser(User user) {
        userService.delUser(user);
        return new Result();
    }

    @RequestMapping("/editPage")
    public ModelAndView editUser(User user) {
        User rsUser = userService.getUserById(user);
        Map<String, Object> rsMap = new HashMap<>();
        rsMap.put("data", rsUser);
        return new ModelAndView("edit", rsMap);

    }

    @RequestMapping("/update")
    public Result updateUser(User user) {
        userService.updateUser(user);
        return new Result();
    }
}
